﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThirdPartyLibraries;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using System.Drawing;
using System.Xml;
using PPIDesktopClient.Logic;
using System.IO;
using PPIDesktopClient.Entities.Kml;
using PPIDesktopClient.Interface;

namespace ThirdPartyLibraries.GoogleEarth.Classes
{
    public class GEManager
    {
        public GEManager(GoogleEarthViewPort viewPort)
        {
            m_KmlFiles = new List<EKmlFile>();
            m_ViewPort = viewPort;
        }
        public GEManager(GoogleEarthViewPort viewPort, List<EUtility_Polygon> poligons, List<EUtility_QueryAsset> queryAssets, List<EUtility_QueryPolygon> queryPolygon)
            : this(viewPort)
        {
            m_poligons = poligons;
            m_queryAssets = queryAssets;
            m_queryPolygon = queryPolygon;
        }
        public GEManager(GoogleEarthViewPort viewPort, List<EUtility_Polygon> poligons, List<EUtility_QueryAssetShown> queryAssetsShown, List<EUtility_QueryPolygon> queryPolygon)
            : this(viewPort)
        {
            m_poligons = poligons;
            m_queryAssetsShown = queryAssetsShown;
            m_queryPolygon = queryPolygon;
        }
        public GEManager(GoogleEarthViewPort viewPort, List<EUtility_Polygon> poligons, List<EUtility_QueryAssetShown> queryAssetsShown, List<EUtility_QueryPolygon> queryPolygon, List<EVendor_Visit_XMissionStructure> queryMobile)
            : this(viewPort, poligons, queryAssetsShown, queryPolygon)
        {
            m_queryMobile = queryMobile;
        }


        protected List<EKmlFile> m_KmlFiles;
        protected GoogleEarthViewPort m_ViewPort;

        protected EUtility_Asset_Shown m_UtilityAssets;
        protected List<EUtility_Polygon> m_poligons;
        protected List<EUtility_QueryAsset> m_queryAssets;
        protected List<EUtility_QueryAssetShown> m_queryAssetsShown;
        protected List<EUtility_QueryPolygon> m_queryPolygon;
        protected List<EVendor_Visit_XMissionStructure> m_queryMobile;
        protected EDistancePositions m_DistancePositions;
        protected EBaseAsset m_SingleVisit;



        public List<EUtility_Polygon> M_poligons
        {
            get
            {
                return m_poligons;
            }
            set
            {
                if (m_poligons == value)
                    return;
                m_poligons = value;
            }
        }
        public List<EUtility_QueryAsset> M_queryAssets
        {
            get
            {
                return m_queryAssets;
            }
            set
            {
                if (m_queryAssets == value)
                    return;
                m_queryAssets = value;
            }
        }
        public List<EUtility_QueryPolygon> M_queryPolygon
        {
            get
            {
                return m_queryPolygon;
            }
            set
            {
                if (m_queryPolygon == value)
                    return;
                m_queryPolygon = value;
            }
        }
        public List<EVendor_Visit_XMissionStructure> M_queryMobile
        {
            get
            {
                return m_queryMobile;
            }
            set
            {
                m_queryMobile = value;
            }
        }
        public EUtility_Asset_Shown UtilityAssets
        {
            get
            {
                return m_UtilityAssets;
            }
            set
            {
                m_UtilityAssets = value;
            }
        }
        public EDistancePositions DistancePositions
        {
            get
            {
                return m_DistancePositions;
            }
            set
            {
                m_DistancePositions = value;
            }
        }
        public EBaseAsset SingleVisit
        {
            get { return m_SingleVisit; }
            set { m_SingleVisit = value; }
        }


        protected bool m_showAssets;
        protected bool m_showPolygons;
        public bool M_showAssets
        {
            get
            {
                return m_showAssets;
            }
            set
            {
                if (m_showAssets == value)
                    return;
                m_showAssets = value;
            }
        }
        public bool M_showPolygons
        {
            get
            {
                return m_showPolygons;
            }
            set
            {
                if (m_showPolygons == value)
                    return;
                m_showPolygons = value;
            }
        }


        public GoogleEarthViewPort ViewPort
        {
            set { m_ViewPort = value; }
        }


        public void ExecuteKML()
        {
            //CleanKML();
            if (m_showAssets)
            {
                //QueryAssetsToKmlFile(m_queryAssetsShown);
                AssetsToKmlfile(m_queryMobile, "Blue", EUtility_QueryAssetTextShow.Internal_Utility_ID);
                PolesToKmlfile(m_queryMobile, "Blue");
                if (m_DistancePositions != null)
                    ObjectToKml(m_DistancePositions);
            }
            //if (m_showPolygons)
            //{
            //    PolygonsToKmlfile(m_poligons);
            //    QueryPolygonsToKmlFile(m_queryPolygon);
            //}

            string _KMLCode = GEHelper.GenerateKMLFile(ref m_KmlFiles);
            GEHelper.WriteKMLFile(_KMLCode, Constants.k_Filename);
            m_ViewPort.OpenKmlFile(Constants.k_Filename, 1);
            m_KmlFiles.Clear();
        }
        
        public void ExecuteSingleKML()
        {
            //CleanKML();
            AssetsToKmlfile(m_SingleVisit,m_SingleVisit.Icon,m_SingleVisit.TextToShow);
            string _KMLCode = GEHelper.GenerateKMLFile(ref m_KmlFiles);
            GEHelper.WriteKMLFile(_KMLCode, Constants.k_Filename);
            m_ViewPort.OpenKmlFile(Constants.k_Filename, 1);
            m_KmlFiles.Clear();
        }

        public void ExecuteUtilityKML()
        {
            //CleanKML();
            if (m_showAssets)
            {
                AssetsToKmlfile(m_UtilityAssets.VendorVisit, "Green", EUtility_QueryAssetTextShow.Internal_Utility_ID);
                AssetsToKmlfile(m_UtilityAssets.NearAssets, "Blue", EUtility_QueryAssetTextShow.Internal_Utility_ID);
            }
            string _KMLCode = GEHelper.GenerateKMLFile(ref m_KmlFiles);
            GEHelper.WriteKMLFile(_KMLCode, Constants.k_Filename);
            m_ViewPort.OpenKmlFile(Constants.k_Filename, 1);
            m_KmlFiles.Clear();
        }

        public void ExecuteGPSProject(List<EVendor_Visit_XMissionStructure> visits)
        {
            m_KmlFiles.Clear();
            AssetsToKmlfile(visits, "Blue", EUtility_QueryAssetTextShow.Internal_Utility_ID);
            string _KMLCode = GEHelper.GenerateKMLFile(ref m_KmlFiles);
            GEHelper.WriteKMLFile(_KMLCode, Constants.k_Filename);
            m_ViewPort.OpenKmlFile(Constants.k_Filename, 1);
            m_KmlFiles.Clear();
        }
        public void CleanKML()
        {
            string _KMLFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://earth.google.com/kml/2.1\"></kml>";
            GEHelper.WriteKMLFile(_KMLFile, Constants.k_Filename);
            m_ViewPort.OpenKmlFile(Constants.k_Filename, 1);
        }

        private void PolesToKmlfile(List<EVendor_Visit_XMissionStructures_Pole> poles, string color, EUtility_QueryAssetTextShow title)
        {
            try
            {
                foreach (EVendor_Visit_XMissionStructures_Pole Pole in poles)
                {
                    Pole.Icon = color;
                    Pole.TextToShow = title;
                    m_KmlFiles.Add(Pole.ToKml());
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        private void PolesToKmlfile(List<EUtility_Asset_XMissionStructures_Pole> poles, string color, EUtility_QueryAssetTextShow title)
        {
            try
            {
                foreach (EUtility_Asset_XMissionStructures_Pole p in poles)
                {
                    PolesToKmlfile(p, color, title);
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

        private void PolesToKmlfile(List<EVendor_Visit_XMissionStructure> Assets, string color)
        {
            foreach (EVendor_Visit_XMissionStructure a in Assets)
            {
                foreach (EBasePole p in a.Poles)
                {
                    PolesToKmlfile(p, color, EUtility_QueryAssetTextShow.Internal_Utility_ID);
                }
            }
        }

        private void PolesToKmlfile(List<EUtility_Asset_XMissionStructure> Assets, string color)
        {
            foreach (EUtility_Asset_XMissionStructure a in Assets)
            {
                PolesToKmlfile(a.Poles, color, EUtility_QueryAssetTextShow.Internal_Utility_ID);
            }
        }

        private void PolesToKmlfile(EBasePole pole, string color, EUtility_QueryAssetTextShow title)
        {
            try
            {
                pole.Icon = color;
                pole.TextToShow = title;
                m_KmlFiles.Add(pole.ToKml());
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        private void PolygonsToKmlfile(List<EUtility_Polygon> polygons)
        {
            EKmlFile _KmlFile;
            string points;
            try
            {
                foreach (EUtility_Polygon p in polygons)
                {
                    _KmlFile = new EKmlFile();
                    _KmlFile.KMLCode = GEHelper.GeneratePolygonTypeStyle(p.Utility_PolygonId.ToString(), p.LineWidth.ToString(), getHexaColor(p.LineColor), getHexaColor(p.FillColor));
                    m_KmlFiles.Add(_KmlFile);
                    points = GetpolygonsPoints(p);
                    for (int i = 0; i < p.Utility_Polygon_Points.Length; i++)
                    {
                        _KmlFile = new EKmlFile();
                        _KmlFile.KMLCode = GEHelper.GeneratePolygon(p.Utility_PolygonId.ToString(), p.Name, p.Utility_PolygonId.ToString(), points);
                        m_KmlFiles.Add(_KmlFile);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private string getHexaColor(int color)
        {
            string returnColor;
            Color _ColorLine = System.Drawing.Color.FromArgb((color - color % 1000000) / 1000000, (color % 1000000 - color % 1000) / 1000, color % 1000);
            returnColor = String.Format("{0:x2}", _ColorLine.A) + String.Format("{0:x2}", _ColorLine.B) + String.Format("{0:x2}", _ColorLine.G) + String.Format("{0:x}", _ColorLine.R);
            return returnColor;
        }

        private string GetpolygonsPoints(EUtility_Polygon polygon)
        {
            string _Point = "";
            string _Longitude;
            string _Latitude;
            for (int i = 0; i < polygon.Utility_Polygon_Points.Length; i++)
            {
                //-68.39528200757741,11.69271640081955,0
                _Longitude = polygon.Utility_Polygon_Points[i].Longitude.ToString().Replace(",", ".");
                _Latitude = polygon.Utility_Polygon_Points[i].Latitude.ToString().Replace(",", ".");
                _Point += _Longitude + "," + _Latitude + ",50 ";
            }
            return _Point;
        }

        private void AssetsToKmlfile(List<EVendor_Visit_XMissionStructure> assets, string color, EUtility_QueryAssetTextShow title)
        {
            try
            {
                foreach (EVendor_Visit_XMissionStructure a in assets)
                {
                    AssetsToKmlfile(a, color, title);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void AssetsToKmlfile(List<EUtility_Asset_XMissionStructure> assets, string color, EUtility_QueryAssetTextShow title)
        {

            try
            {
                foreach (EUtility_Asset_XMissionStructure a in assets)
                {
                    AssetsToKmlfile(a, color, title);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void AssetsToKmlfile(EBaseAsset asset, string color, EUtility_QueryAssetTextShow title)
        {
            try
            {
                asset.Icon = color;
                asset.TextToShow = title;
                m_KmlFiles.Add(asset.ToKml());

                foreach (EBaseImage i in asset.Images)
                {
                    m_KmlFiles.Add(i.ToKml());
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        private void QueryPolygonsToKmlFile(List<EUtility_QueryPolygon> queryPolygon)
        {
            List<EUtility_Polygon> _Polygons;
            foreach (EUtility_QueryPolygon qp in queryPolygon)
            {
                _Polygons = LUtility_QueryPolygon.Execute(qp.Utility_QueryPolygonId);
                PolygonsToKmlfile(_Polygons);
            }
        }

        private void QueryAssetsToKmlFile(List<EUtility_QueryAsset> queryAssets)
        {
            List<EVendor_Visit_XMissionStructure> _QueryAssets;
            foreach (EUtility_QueryAsset qas in queryAssets)
            {
                _QueryAssets = LUtility_QueryAsset.Execute(qas.Utility_QueryAssetId);
                AssetsToKmlfile(_QueryAssets, "Red", EUtility_QueryAssetTextShow.Internal_Utility_ID);
                foreach (EVendor_Visit_XMissionStructure asset in _QueryAssets)
                {
                    foreach (EBasePole p in asset.Poles)
                    {
                        PolesToKmlfile(p, "Red", EUtility_QueryAssetTextShow.Internal_Utility_ID);
                    }
                }

            }
        }

        private void QueryAssetsToKmlFile(List<EUtility_QueryAssetShown> queryAssets)
        {
            foreach (EUtility_QueryAssetShown qas in queryAssets)
            {
                if (qas.Assets != null)
                {
                    AssetsToKmlfile(qas.Assets, qas.Color, qas.TextToShow);

                    foreach (EUtility_Asset_XMissionStructure a in qas.Assets)
                    {
                        PolesToKmlfile(a.Poles, qas.Color, qas.TextToShow);
                    }
                }

            }
        }

        private void ObjectToKml(IGoogleEarth o)
        {
            if (!o.IsEmpty())
                m_KmlFiles.Add(o.ToKml());
        }
    }
}
